package org.example.struct.array;

import com.alibaba.fastjson.JSON;

import java.util.ArrayList;
import java.util.List;

/**
 * @author liyishan
 * @date 2023/7/19 09:59
 * @apiNote
 */

public class Test00119GetRow {
    public static void main(String[] args) {
        List<Integer> test = getRow1(5);
        System.out.println("array:" + JSON.toJSON(test));
    }

    public static List<Integer> getRow1(int rowIndex) {
        List<Integer> row = new ArrayList<Integer>();
        row.add(1);
        for (int i = 1; i <= rowIndex; ++i) {
            row.add(0);
            for (int j = i; j > 0; --j) {
                row.set(j, row.get(j) + row.get(j - 1));
            }
        }
        return row;
    }

    /**
     * 滚动数组
     * @param rowIndex
     * @return
     */
    public List<Integer> getRow(int rowIndex) {
        List<Integer> row = new ArrayList<Integer>();
        for (int i = 0; i <= rowIndex; i++) {
            List<Integer> cur = new ArrayList<Integer>();
            for (int j = 0; j <= i; j++) {
                if (j == 0 || j == i) {
                    cur.add(1);
                }else{
                    cur.add(row.get(j - 1) + row.get(j));
                }
            }
            row = cur;
        }

        return row;
    }


    public static List<Integer> test(int rowIndex) {
        List<Integer> row = new ArrayList<>();
        row.add(1);
        for (int i = 1; i <= rowIndex; ++i) {
            row.add((int) ((long) row.get(i - 1) * (rowIndex - i + 1) / i));
        }
        return row;
    }
}
